package edu.base.controller;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import edu.base.model.CatDetail;
import edu.base.model.CmtJoinUser;
import edu.base.model.MaintenanceRecord;
import edu.base.model.MaintenanceRecordJoinUsers;
import edu.base.service.CatDetailService;
import edu.base.service.CategoryService;
import edu.base.service.CmtService;
import edu.base.service.MaintenanceRecordService;
import edu.base.service.UsersService;
import edu.base.util.PageUtil;
import edu.base.config.CmsConfig;
import edu.base.model.Users;


@RequestMapping("/customer")
@Controller
public class CustomerController {
	final static org.slf4j.Logger log = LoggerFactory.getLogger(CustomerController.class);

	@Autowired
	UsersService usersService; 
	@Autowired
	CategoryService categoryService;
	@Autowired
	CatDetailService catDetailService;
	@Autowired
	MaintenanceRecordService maintenanceRecordService;
	@Autowired
	CmtService cmtService;
	
	@RequestMapping("index")
	public String index() {
		return "/customer/index";
	}

	@RequestMapping("welcome")
	public String welcome() {
		return "/admin/welcome";
	}
	
	@RequestMapping("record/lst")
	public String recordLst(Integer p,String startDate, String endDate,Integer state,Integer customerId,Model model,HttpSession session) throws ParseException {
		Date date1=null;
		Date date2 = null;
		if(startDate != null) {
			date1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
		}
		if(endDate != null) {
			date2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
		}
		if(p == null) {
			p = 1;
		}
		customerId = (Integer) session.getAttribute("user_id");
		List<MaintenanceRecordJoinUsers> list = maintenanceRecordService.query(date1, date2, customerId, null, state, p, 10);
		Integer count = maintenanceRecordService.count(date1, date2, customerId, null, state);
		PageUtil page = new PageUtil(10, count, p, "/admin/record/lst?");
		
		model.addAttribute("list", list);
		model.addAttribute("p", p);
		model.addAttribute("startDate",startDate);
		model.addAttribute("endDate", endDate);
		model.addAttribute("state", state);
		model.addAttribute("customerId", customerId);
		model.addAttribute("page", page.page());
		return "/customer/record/lst";
	}
	
	@RequestMapping("record/detail")
	public String RecordDetail(Integer id,HttpSession session,Model model) {
		MaintenanceRecord item = maintenanceRecordService.findByKey(id);
		List<CatDetail> lst = catDetailService.find(null, null, false, id);
		List<CmtJoinUser> cmtLst = cmtService.topicCmt(id);
		
		model.addAttribute("cmt_lst", cmtLst);
		model.addAttribute("lst", lst);
		model.addAttribute("item", item);
		return "/customer/record/detail";
	}
	
	
	@RequestMapping("user_edit")
	public String userEdit(Model model, HttpSession session) {
		edu.base.model.Users user = usersService.findByKey((Integer) session.getAttribute("user_id"));
		model.addAttribute("user", user);
		return "/customer/user_edit";
	}
	
	@RequestMapping("do_user_edit")
	public String doUserEdit(String login_pwd, MultipartFile avatar, String nick, String email,String tel, HttpSession session)
			throws IllegalStateException, IOException {
		log.info("用户信息修改 参数：" + login_pwd + avatar);
		Users user = new Users();
		user.setLoginPwd(DigestUtils.md5DigestAsHex(login_pwd.getBytes()));
		if(!avatar.isEmpty()) {
			String savePath = CmsConfig.getUploadPath();
	    	String fileName = avatar.getOriginalFilename();
	    	String ext = fileName.substring(fileName.lastIndexOf("."),
					fileName.length());
	    	String saveName = System.currentTimeMillis() + ext;
	    	File saveFile = new File(savePath + saveName);
	    	log.info("save_name:" + saveFile.getAbsolutePath());
	    	avatar.transferTo(saveFile);
	    	user.setAvatar("/images/" + saveName);
    	}
		user.setNick(nick);
		user.setEmail(email);
		user.setUserId((Integer) session.getAttribute("user_id"));
		user.setTel(tel);
		
		log.info("用户信息修改：" + user.toString());
		usersService.update(user);
		user = usersService.findByKey(user.getUserId());
		session.setAttribute("user", user);
		return "redirect:/customer/user_edit";
	}

	
}
